package uutalk.netcloud.talk.base.util;


import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.util.Log;

import java.io.IOException;

/**
 * Created by Administrator on 2018/4/10.
 * 音频播放类
 */


public class MediaManager {



	private static MediaPlayer mPlayer;
	
	private static boolean isPause;


	public static  void playSound(String filePathString,
			OnCompletionListener onCompletionListener) {
		String url = "http://onyyki115.bkt.clouddn.com/" + filePathString;
		if (mPlayer==null) {
			mPlayer=new MediaPlayer();
			//保险起见，设置报错监听
			mPlayer.setOnErrorListener(new OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {

					mPlayer.reset();
					return false;
				}
			});
		} else {
			mPlayer.reset();//就回复
		}

		try {
			mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mPlayer.setOnCompletionListener(onCompletionListener);
			Log.e("GroupHistoryAdapter",url);
			mPlayer.setDataSource(url);
			mPlayer.prepare();
			mPlayer.start();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//停止函数
	public static void pause(){
		if (mPlayer!=null&&mPlayer.isPlaying()) {
			mPlayer.pause();
			isPause=true;
		}
	}
	
	//继续
	public static void resume()
	{
		if (mPlayer!=null&&isPause) {
			mPlayer.start();
			isPause=false;
		}
	}


	public  static void release()
	{
		if (mPlayer!=null) {
			mPlayer.release();
			mPlayer=null;
		}
	}

	public static boolean isPlaying() {
		if (mPlayer != null && mPlayer.isPlaying()) {
			return true;
		}
		return false;
	}

}
